<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline label-width="90px">
                <el-form-item label="串号" prop="imei">
                    <el-input class="w-[280px]" v-model="queryParams.imei" clearable placeholder="请输入串号" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select class="w-[280px]" style="width: 280px" v-model="queryParams.status" clearable placeholder="请选择状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.repair_status" :key="index" :label="item.name" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="报修单号" prop="order_no">
                    <el-input class="w-[280px]" v-model="queryParams.order_no" clearable placeholder="请输入报修单号" />
                </el-form-item>
                <el-form-item label="完成时间" prop="finish_repari_time">
                    <daterange-picker v-model:startTime="queryParams.start_time" v-model:endTime="queryParams.end_time" />
                </el-form-item>

                <el-form-item label="报修说明" prop="remark">
                    <el-input class="w-[280px]" v-model="queryParams.remark" clearable placeholder="请输入报修说明" />
                </el-form-item>
                <el-form-item label="快递单号" prop="express_no">
                    <el-input class="w-[280px]" v-model="queryParams.express_no" clearable placeholder="快递单号" />
                </el-form-item>
                <el-form-item label="快递公司" prop="express_type">
                    <el-select class="w-[280px]" style="width: 280px" v-model="queryParams.express_type" clearable placeholder="请选择快递公司">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.express" :key="index" :label="item.name" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="回寄单号" prop="receive_express_no">
                    <el-input class="w-[280px]" v-model="queryParams.receive_express_no" clearable placeholder="请输入回寄单号" />
                </el-form-item>
                <el-form-item label="回寄地址" prop="receive_address">
                    <el-input class="w-[280px]" v-model="queryParams.receive_address" clearable placeholder="请输入回寄地址" />
                </el-form-item>
                <el-form-item label="回寄联系人" prop="receive_concate_name">
                    <el-input class="w-[280px]" v-model="queryParams.receive_concate_name" clearable placeholder="请输入回寄联系人" />
                </el-form-item>
                <el-form-item label="回寄电话" prop="receive_concate_phone">
                    <el-input class="w-[280px]" v-model="queryParams.receive_concate_phone" clearable placeholder="请输入回寄电话" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <!-- <el-button v-perms="['repair/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
新增
</el-button> -->
            <el-button v-perms="['repair/delete']" :disabled="!selectData.length" @click="handleDelete(selectData)"> 删除 </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="用户" prop="user_id">
                        <template #default="{ row }">
                            <div>
                                <el-avatar :src="row.user.avatar" :size="50" />
                            </div>

                            <span class="ml-2">{{ row.user.nickname }}</span>
                            <span class="ml-2">{{ row.user.mobile }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="串号" prop="imei" />
                    <el-table-column label="手机型号" prop="spec" />
                    <el-table-column label="品牌" prop="brand" />
                    <el-table-column label="状态" prop="status">
                        <template #default="{ row }">
                            {{ row.status_desc }}
                        </template>
                    </el-table-column>
                    <el-table-column label="报修单号" prop="order_no" />
                    <el-table-column label="支付信息" prop="imei">
                        <template #default="{ row }">
                            <div class="flex flex-col">
                                <span>支付方式：{{ row.pay_way == 1 ? '微信' : '其他' }}</span>
                                <span>支付时间：{{ row.pay_time }}</span>
                                <span>支付金额：{{ row.pay_price }}</span>
                                <span>交易号：{{ row.transaction_id }}</span>
                                <span>退款订单号：{{ row.refund_no }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="客户邮寄信息" prop="express_no">
                        <template #default="{ row }">
                            <div class="flex flex-col">
                                <span>快递单号：{{ row.express_no }}</span>
                                <span>快递公司：{{ row.express_type }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="回寄信息" prop="receive_express_no">
                        <template #default="{ row }">
                            <div class="flex flex-col">
                                <span>回寄单号：{{ row.receive_express_no }}</span>
                                <span>回寄地址：{{ row.receive_address }}</span>
                                <span>回寄联系人：{{ row.receive_concate_name }}</span>
                                <span>回寄电话：{{ row.receive_concate_phone }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="审核结果" prop="receive_express_no">
                        <template #default="{ row }">
                            <div class="flex flex-col"></div>
                            <span>审核结果：{{ row.check_remark }}</span>
                            <span>审核时间：{{ row.update_time }}</span>
                            <span>
                                <el-image v-for="(item, index) in row.check_img_arr" :preview-teleported="true" :preview-src-list="row.check_img_arr" :key="index" :src="item" fit="fill" :lazy="true" :width="100" :height="100" />
                            </span>
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-if="row.pay_status == 1 && row.status == 15" type="primary" link @click="handleFish(row)"> 完成 </el-button>

                            <el-button v-if="row.pay_status == 1 && row.status == 15" type="primary" link @click="refund(row)"> 拒绝 </el-button>
                            <el-button v-perms="['repair/delete']" type="danger" link @click="handleDelete(row.id)"> 删除 </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <finish v-if="showFinish" ref="finishRef" :dict-data="dictData" @success="getLists" @close="showFinish = false" />
        <refuse ref="refuseRef" :dict-data="dictData" @success="getLists" @close="showRefuse = false" />
    </div>
</template>

<script lang="ts" setup name="repairLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiRepairHuanLists, apiRepairDelete } from '@/api/repair'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'

import finish from './finish.vue'
const finishRef = shallowRef<InstanceType<typeof finish>>()
// 是否显示完成框
const showFinish = ref(false)
const handleFish = async (data: any) => {
    showFinish.value = true
    await nextTick()
    finishRef.value?.open('edit')
    finishRef.value?.setFormData(data)
}

import refuse from './refuse.vue'
const refuseRef = shallowRef<InstanceType<typeof refuse>>()
// 是否显示拒绝框
const showRefuse = ref(false)

const refund = async (data: any) => {
    await nextTick()
    refuseRef.value?.open('edit')
    refuseRef.value?.setFormData(data)
}

// 查询条件
const queryParams = reactive({
    imei: '',
    status: '',
    order_no: '',
    finish_repari_time: '',
    remark: '',
    express_no: '',
    express_type: '',
    receive_express_no: '',
    receive_address: '',
    receive_concate_name: '',
    receive_concate_phone: '',
    start_time: '',
    end_time: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('express,repair_status')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiRepairHuanLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiRepairDelete({ id })
    getLists()
}

getLists()
</script>
